package com.guanjia.web;

import com.guanjia.entity.CreditInformation;
import com.guanjia.entity.vo.Image;
import com.guanjia.entity.vo.ImageCorr;
import com.guanjia.mapper.CreditinfoMapper;
import com.guanjia.mapper.ImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * 借贷  各个资产的图片上传
 */
@Controller
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private CreditinfoMapper creditinfoMapper;



    @ResponseBody
    @RequestMapping("/contract")
    public String contractUpload(@RequestParam("contract") MultipartFile[] multipartfiles, HttpSession session) throws IOException {
        int credit=Integer.parseInt(session.getAttribute("credit_id")+"");
        uploadUtils(multipartfiles,"contract",credit);
        return "1";
    }

    @ResponseBody
    @RequestMapping("/idCard")
    public String idCardUpload(@RequestParam("img1") MultipartFile[] multipartfiles, HttpSession session) throws IOException {
        int credit=Integer.parseInt(session.getAttribute("credit_id")+"");
       uploadUtils(multipartfiles,"idCard",credit);
        return "1";
    }
    @ResponseBody
    @RequestMapping("/car")
    public String carUpload(@RequestParam("img2")MultipartFile[] multipartfiles,HttpSession session)throws IOException {
        int credit=Integer.parseInt(session.getAttribute("credit_id")+"");
      uploadUtils(multipartfiles,"car",credit);
        return "1";
    }
    @ResponseBody
    @RequestMapping("/home")
    public String homeUpload(@RequestParam("img3")MultipartFile[] multipartfiles,HttpSession session)throws IOException {
        int credit=Integer.parseInt(session.getAttribute("credit_id")+"");
       uploadUtils(multipartfiles,"home",credit);
        return "1";
    }

    @ResponseBody
    @RequestMapping("/other")
    public String otherUpload(@RequestParam("img4")MultipartFile[] multipartfiles,HttpSession session)throws IOException {
        int credit=Integer.parseInt(session.getAttribute("credit_id")+"");
        uploadUtils(multipartfiles,"orther",credit);

    return "1";
    }

    public void uploadUtils(MultipartFile[] multipartfiles,String type,int credit) throws IOException {
          String uname="";

            UserDetails userDetails=null;

                userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                uname=userDetails.getUsername()+"/";

        if(multipartfiles != null && multipartfiles.length>0){
            for(MultipartFile item : multipartfiles)
            { String fileName = item.getOriginalFilename();
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                String newfileName=UUID.randomUUID().toString() +"."+suffix;
                //获取文件名
                String filePath = "F:/picall/cfgj/"+uname+type+"/" ;
                //自定义上传路径
                File file = new File(filePath,newfileName);
                if(!file.exists()){//判断文件夹是否存在，如果不存在则创建
                    if(!file.getParentFile().exists()){
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                item.transferTo(file);//上传文件
                if(type.equals("contract")){
                    CreditInformation creditInformation=creditinfoMapper.findCreditInfoById(credit);
                    creditInformation.setAgreementURL(uname+type+"/"+newfileName);
                    creditinfoMapper.updateById(creditInformation);

                }else {
                Image image=new Image(uname+type+"/"+newfileName);
                imageMapper.insert(image);
                ImageCorr corr=new ImageCorr(credit,Integer.parseInt(image.getId()));
                if(type.equals("idCard")){
                    imageMapper.insertidCardimg(corr);
                }
                if(type.equals("car")){
                    imageMapper.insertCarimg(corr);
                }
                if(type.equals("home")){
                    imageMapper.inserthouseimg(corr);
                }
                if(type.equals("other")){
                    imageMapper.insertotherimg(corr);
                }

            }
            }
        }

    }

}
